<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of lang_model
 *
 * @author Truong
 */
class lang_model extends backend_model {

    function __construct()
    {
        parent::__construct('language','lang');
		
    }
    function getbykey($id){
        $tmp = $this->lang_model->_get($id);
        $items=null;
        if($tmp){
            $items['lang_key']=$tmp->lang_key;
            $query=$this->db
                    ->where('lang_key',$tmp->lang_key)
                    ->where_in('lang_language',array('en','vi'))
                    ->get('language');
            $rows=$query->result();
            foreach ($rows as $row){
                $items['text'][$row->lang_language]=$row->lang_text;
            }
        }
        return $items;
    }
    function insert_on_duplicate($ai,$au){
        $this->db->on_duplicate_update('language', $au, $ai); 
        $count = $this->db->affected_rows();
        $error_number = $this->db->_error_number();
        $error_message = $this->db->_error_message();
        if($error_number!==0){
            $_SESSION['auth_db']['errors'][]=$error_message;
            return false;
        }
        if($count>0) return $count;
        return false;
    }
    function colmod(){
        return array(
            array(  
                "text"          =>"#"            
                ,"datafield"    =>"_id" 
                //,"cellsalign"   =>"right"
                ,"width"        => 60 
                ,"filterable"       =>false
                ),
            array(  
                "text"          =>"Keywork"            
                ,"datafield"    =>"lang_key" 
                //,"cellsalign"   =>"right"
                ,"width"        => 120 
                ,"editable"       =>true
                ),
            array(  
                "text"          =>"Text"            
                ,"datafield"    =>"lang_text" 
                //,"width"        => 220
                ,"editable"     =>true
                ),
             array(  
                "text"          =>"Page"            
                ,"datafield"    =>"lang_set" 
                ,"width"        => 80
                ,"editable"     =>true
                )
            
        );
    }
    function binding($lang='en'){
        
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS `language`.*,`language`.`lang_id` as _id
            FROM `language`
                
            ";
        $strWhere="
            WHERE  true
            ";
        if($lang){$lang=  $this->db->escape_str($lang);$strWhere.=" AND `lang_language` = '$lang'";}
        $strOrderBy="ORDER BY `lang_key` ASC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>false,
            "fields"=>array(),
            "datefields"=>array()
        );
        $this->init($config);
        return $this->jqxBinding();
    }
}

?>
